package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Device;
import com.sl.au.entity.SinglePump;
import com.sl.au.entity.Weekly;
import com.sl.au.repository.DeviceRepository;
import com.sl.au.service.DeviceService;
import com.sl.au.service.ExcelService;
import com.sl.au.service.SinglePumpService;
import com.sl.utils.CRM;

@Service("deviceService")
@Transactional
public class DeviceServiceImpl extends BaseServiceImpl implements DeviceService {

	@Autowired
	DeviceRepository deviceRepository;
	
	@Autowired
	SinglePumpService singlePumpService;

	@Autowired
	ExcelService excelService;

	@Override
	public List<Device> getPage(int start, int length, String key, String orderName, String dir) {
		Page<Device> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = deviceRepository.getSinglePump(key, pageable);
		return pages.getContent();
	}

	public DeviceRepository getRepository() {
		return this.deviceRepository;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public long importExcel(String fileName) {
		long count = 0;
		if (!StringUtils.hasText(fileName))
			return count;
		Map<String, Object> collections = new HashMap<>();
		// excelService.setStorageLocation(CRM.getStorageLocation());
		excelService.importExcel("device", collections, fileName);
		if (collections.size() < 1 || collections.get("items") == null)
			return count;
		List<Device> rows = (List<Device>) collections.get("items");
		List<Device> devices=this.getCheckedDevice(rows);
		List<SinglePump> singlePumps = new ArrayList<SinglePump>();
		if (null != devices && devices.size() > 0) {
			for(Device d:devices){
				String type=d.getDeviceType();
				SinglePump s1 = new SinglePump();
				s1.setDevice(d);
				s1.setSuperior("成套处");
				s1.setType(type);
				singlePumps.add(s1);
				SinglePump s2 = new SinglePump();
				s2.setDevice(d);
				s2.setSuperior("外贸处");
				s2.setType(type);
				singlePumps.add(s2);
				SinglePump s3 = new SinglePump();
				s3.setDevice(d);
				s3.setSuperior("销售处");
				s3.setType(type);
				singlePumps.add(s3);
				SinglePump s4 = new SinglePump();
				s4.setDevice(d);
				s4.setSuperior("售后服务处");
				s4.setType(type);
				singlePumps.add(s4);
			}
			this.deviceRepository.save(devices);
			singlePumpService.getRepository().save(singlePumps);
			count = devices.size();
		}
		return count;
	}

	@Override
	public List<Device> getCheckedDevice(List<Device> rows) {
		List<Device> devices=new ArrayList<Device>();
		for(Device d:rows){
			String deviceType=d.getDeviceType();
			long count=this.getRepository().getReCount(deviceType);
			if(count==0){
				if(d.getFlow()==null){
					d.setFlow((float) 0.0);
				}
				if(d.getPressure()==null){
					d.setPressure((float) 0.0);
				}
				devices.add(d);
			}
		}
		return devices;
	}

	@Override
	public void patch(Device device) {
		Device d=this.getRepository().getOne(device.getId());
		if(null==d) return;		
		BeanUtils.copyProperties(device, d,"createTime");
		this.getRepository().save(d);
		String deviceId=d.getId();
		String type=d.getDeviceType();
		singlePumpService.getRepository().updateTypeByDeviceId(type, deviceId);
		
	}
}